package com.shenghui.enterprise.base.controller;

import com.shenghui.enterprise.base.entity.BaseRole;
import com.shenghui.enterprise.utils.GeneralUtil;
import com.shenghui.enterprise.vo.Result;
import com.shenghui.enterprise.base.service.IBaseRoleService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 角色管理 前端控制器
 * @Author houjh
 * @Email: happyxiaohou@gmail.com
 * @Date: 2020-05-29
 * @Copyright (c) 2020-2022 云南昇晖科技有限公司
 */
@RestController
@RequestMapping("/admin/baseRole" )
@Api(value = "角色管理接口",description = "用作角色管理演示")
public class BaseRoleController {

    @Autowired
    private IBaseRoleService baseRoleService;

    /**
     * 查询列表信息
     * @return
     */
    @RequestMapping("/getBaseRoleList")
    public Object getBaseRoleList(@RequestParam Map map){
        return baseRoleService.getBaseRoleList(map);
    }

    /**
     * 删除角色信息
     * @param ids
     * @return
     */
    @RequestMapping("/deleteBaseRole")
    public Result deleteBaseRole(String ids) {
        if (GeneralUtil.isNull(ids)) {
            return Result.error("参数【id】不能为空");
        }
        Result result = baseRoleService.deleteBaseRole(ids);
        return result;
    }

    /**
     * 修改角色排序值
     * @return
     */
    @RequestMapping("/updateBaseRoleOrder")
    public Object updateBaseRoleOrder(String id, Integer orderValue) {
        return baseRoleService.updateBaseRoleOrder(id, orderValue);
    }

    /**
     * 添加角色信息
     * @return
     */
    @RequestMapping("/addBaseRole")
    public Result addBaseRole(BaseRole role, String id) {
        Result result = baseRoleService.addBaseRole(role, id);
        return result;
    }

    /**
     * 根据id查询角色详情信息
     * @param id
     * @return
     */
    @RequestMapping("/getBaseRoleById")
    public Result getBaseRoleById(String id){
        if (StringUtils.isEmpty(id)) {
            return Result.error("必填参数【id】不能为空");
        }
        BaseRole role = baseRoleService.getBaseRoleById(id);
        if (GeneralUtil.isNotNull(role)) {
            return Result.ok("查询成功", role);
        } else {
            return Result.error("未查询到对应的角色记录");
        }
    }

    /**
     * 获取权限操作树节点
     * @return
     */
    @RequestMapping("/getAuthorTree")
    public Object getAuthorTree(String roleId, HttpServletRequest request, @RequestParam HashMap map){
        if (StringUtils.isEmpty(roleId)) {
            return null;
        }
        return baseRoleService.getAuthorTree(roleId, request, map);
    }

    /**
     * 添加角色授权信息
     * @param
     * @return
     */
    @RequestMapping("/addAuthorize")
    public Result addAuthorize(String roleId, String moduleId){
        return baseRoleService.addAuthorize(roleId, moduleId);
    }

    /**
     * 取消角色授权
     * @param
     * @return
     */
    @RequestMapping("/cancelAuthorize")
    public Result cancelAuthorize(String roleId, String moduleId){
        return baseRoleService.cancelAuthorize(roleId, moduleId);
    }

}
